require('module-alias').addAlias('~/src', __dirname + '/')

import fs from 'fs';
import axios from 'axios';
import qs from 'qs';
import path from 'path';
import express from 'express';
import { createProxyMiddleware as proxy } from 'http-proxy-middleware';
import cookie from "cookie-parser";
import { translate } from "./ai.js";
var compression = require('compression');

var MysKeyTranslate = require("./MysKeyTranslate");

const translate_baidu = new MysKeyTranslate({
  appid: '20230905001805297',
  secret: 'TWU7axdaKQiRwMeIzXH7',
})

// var text = "Втората световна война беше към края си.";
// var options = {
//   from: 'bul',
//   to: 'zh',
// };

// translate_baidu(text, options).then((result: any) => {
//   console.log(result);
// })

const _delay = async (time: any) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(null);
    }, time);
  });
}

if (false) {


  const proc = async () => {
    //var data = fs.readFileSync("e:\\e1.txt");

    var contentText = fs.readFileSync('C:/Users/zsh/Downloads/Turning.Red.2022.Disney+/8_English.srt', 'utf-8');
    var lines = contentText.split("\n");
    var i = 0;

    var status = 0;

    var accu = "";
    for (var i = 0; i < lines.length; i++) {
      var line = lines[i];
      line = line.replace("\r", "");
      var proc = false;
      switch (status) {
        case 0:
          status = 1;
          proc = false;
          break;
        case 1:
          status = 2;
          proc = false;
          break;
        case 2:
          if (line == "") {
            status = 0;
            proc = true;
          } else {
            accu += line + " ";
            line = "";
            proc = false;
          }
          break;
      }


      var result;
      if (proc) {
        // while (true) {
        //   var ret: any = await axios.post('https://aidemo.youdao.com/trans', { q: accu, from: "bg", to: "zh-CHS", }, {
        //     headers: {},
        //     transformRequest: [function (data) {
        //       var a = qs.stringify(data);
        //       return a;
        //     }],
        //   });
        //   if (ret.data.translation) {
        //     console.log(i + " " + ret.data.translation[0]);
        //     await _delay(5000);
        //     result = ret.data.translation[0] + "\r\n\r\n";
        //     break;
        //   } else {
        //     if (ret.data.errorCode == "103") {
        //       process.exit(0);
        //     }
        //     console.log(ret.data);
        //     await _delay(10000);
        //   }
        // }


        var text = accu;
        var options = {
          from: 'en',
          to: 'zh',
        };

        var ret = await translate_baidu(text, options);
        result = ret + "\r\n\r\n";
        console.log(result);
        await _delay(500);
        fs.appendFileSync('e:\\translate\\ce.txt', text + "\r\n" + result);
        accu = "";
      } else {
        if (line) {
          result = line + "\r\n";
          fs.appendFileSync('e:\\translate\\ce.txt', result);
        }
      }
    }


  };

  proc().then();

  setInterval(() => {
    console.log(".");
  }, 10000000);
}

if (1) {

  var lineCount = 100;

  const proc = async () => {

    //var data = fs.readFileSync("e:\\e1.txt");

    var contentText = fs.readFileSync('e:\\translate\\e7.txt', 'utf-8');

    var lines = contentText.split("\r\n");
    for (var i = 0; i < lineCount; i++) {
      if (lines[i]) {
        while (true) {
          var ret = await axios.post('https://aidemo.youdao.com/trans', { q: lines[i], from: "en", to: "zh-CHS", }, {
            headers: {},
            transformRequest: [function (data) {
              var a = qs.stringify(data);
              return a;
            }],
          });
          if (ret.data.translation) {
            console.log(i + " " + lines[i]);
            console.log(i + " " + ret.data.translation[0]);

            // var my;
            // my = await translate(lines[i], 'qwen2:7b');console.log(i + " " + my);
            // //my = await translate(lines[i], 'llama3.2:3b');console.log(i + " " + my);
            // // my = await translate(lines[i], 'qwen2:1.5b');console.log(i + " " + my);
            // // my = await translate(lines[i], 'qwen:7b');console.log(i + " " + my);
            // // my = await translate(lines[i], 'qwen:4b');console.log(i + " " + my);
            // // my = await translate(lines[i], 'llama3.2:1b');console.log(i + " " + my);
            // // deepseek-coder-v2:16b    63fb193b3a9b    8.9 GB    3 days ago
            // // qwen2:7b                 dd314f039b9d    4.4 GB    3 days ago
            // // qwen:7b                  2091ee8c8d8f    4.5 GB    3 days ago
            // // llama3.2:3b              a80c4f17acd5    2.0 GB    3 days ago
            // // qwen:4b                  d53d04290064    2.3 GB    3 days ago
            // // llama3.2:1b              baf6a787fdff    1.3 GB    3 days ago
            // // qwen2:1.5b               f6daf2b25194    934 MB    3 days ago
            // // qwen:1.8b                b6e8ec2e7126    1.1 GB    3 days ago
            // // qwen:0.5b                b5dc5e784f2a    394 MB    3 days ago
            // // deepseek-r1:7b           0a8c26691023    4.7 GB    7 days ago
            // // deepseek-r1:1.5b         a42b25d8c10a    1.1 GB    7 days ago


            await _delay(5000);
            break;
          } else {
            if (ret.data.errorCode == "103") {
              process.exit(0);
            }
            console.log(ret.data);
            await _delay(10000);
          }
        }
        var result = lines[i] + "\r\n";
        result += ret.data.translation[0] + "\r\n";
        fs.appendFileSync('e:\\translate\\ce.txt', result);
      } else {
        var result = "\r\n";
        result += "\r\n";
        fs.appendFileSync('e:\\translate\\ce.txt', result);
      }
    }


  };

  proc().then();

  setInterval(() => {
    console.log(".");
  }, 10000000);
}

